package com.admin.common.util;

import com.admin.common.constant.BizConstant;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 查询使用的配置文件环境
     *
     * @return
     */
    public static String getActiveProfile() {
        String[] profiles = context.getEnvironment().getActiveProfiles();
        if (!ArrayUtils.isEmpty(profiles)) {
            return profiles[0];
        }
        return "";
    }

    /**
     * 判断当前环境是否为生产
     * @return
     */
    public static boolean isProduct(){
       if( BizConstant.PROFILE_PROD_1.equals(SpringContextUtil.getActiveProfile())
               || BizConstant.PROFILE_PROD_2.equals(SpringContextUtil.getActiveProfile())){
           return  true;
       }
       return false;
    }

    /**
     * 使用的配置文件是否为正式环境
     *
     * @return
     */
    public static boolean isProdOfActiveProfile() {
        String activeProfile = getActiveProfile();
        boolean isProd = false;
        if (BizConstant.PROFILE_PROD_1.equalsIgnoreCase(activeProfile)
            || BizConstant.PROFILE_PROD_2.equalsIgnoreCase(activeProfile)) {
            isProd = true;
        }
        return isProd;
    }
}
